package com.ruoyi.mq

import cn.hutool.core.util.StrUtil
import com.ruoyi.app.common.Sign
import com.ruoyi.back.service.UserRecordService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Component

@Component
class ChangeFaceTaskService {

    private val log: Logger = LoggerFactory.getLogger(ChangeFaceTaskService::class.java)

    @Autowired
    private lateinit var userRecordService: UserRecordService

    @Autowired
    private lateinit var userRecordApiFunc: UserRecordApiFunc

    @Autowired
    private lateinit var changeFaceExtendService: ChangeFaceExtendService

    @Async("threadPoolTaskExecutor")
    fun scheduleTask(id: Long) {
        log.info("线程 --> {} 开始执行换脸任务 {}", Thread.currentThread().name, id)
        userRecordService.getById(id)
            ?.let { userRecord ->
                if (!userRecordApiFunc.check(userRecord)) {
                    log.info("begin resolve task! -> {}", userRecord.id)

                    if (changeFaceExtendService.checkTask(userRecord)) {
                        when (userRecord.apiType) {
                            1 -> {
                                Sign.asyncTask(userRecord.requestId).let { json ->
                                    if (StrUtil.isBlank(json)) {
                                        userRecordApiFunc.hs(userRecord)
                                    } else {
                                        userRecordApiFunc.ali(userRecord, json)
                                    }
                                }
                            }

                            2 -> userRecordApiFunc.hs(userRecord)

                            else -> {
                                log.error("未知的api类型")
                            }
                        }
                    }
                }
            }
    }


}